import isFunciton from './isFunction'
import isFuncitonComponent from './isFunctionComponent'
import mountNativeElement from './mountNatvieElement'

export default function mountComponent (virtualDOM, container) {
    let nextVirtualDOM = null
    // 判断组件是类组件还是函数组件
    if (isFuncitonComponent(virtualDOM)) {
        // console.log('函数组件')
        // 函数组件
        nextVirtualDOM = buildFunctionComponent(virtualDOM)
    }else{
        // 类组件
        nextVirtualDOM = buildClassComponent(virtualDOM)
    }


    if(isFunciton(nextVirtualDOM)){
        mountComponent(nextVirtualDOM, container)
    } else {
        // 挂载到页面中
        mountNativeElement(nextVirtualDOM, container)
    }
}

function buildFunctionComponent (virtualDOM) {
    return virtualDOM.type(virtualDOM.props || {})
}

function buildClassComponent (virtualDOM) {
    const component = new virtualDOM.type(virtualDOM.props || {})
    const nextVirtualDOM = component.render()
    nextVirtualDOM.component = component
    return nextVirtualDOM
}